package me.mononin.factory.preseneter.contact;

import android.support.v7.util.DiffUtil;

import java.util.List;

import me.mononin.common.common.widget.recycler.RecyclerAdapter;
import me.mononin.common.factory.data.DataSource;
import me.mononin.common.factory.presenter.BaseRecyclerPresenter;
import me.mononin.factory.data.helper.UserHelper;
import me.mononin.factory.data.user.ContractDataSource;
import me.mononin.factory.data.user.ContractRepository;
import me.mononin.factory.model.db.User;
import me.mononin.factory.preseneter.BaseSourcePresenter;
import me.mononin.factory.utils.DiffUiDataCallback;

/**
 * 联系人的Presenter实现
 *
 * @author qiujuer Email:qiujuer@live.cn
 * @version 1.0.0
 */
public class ContactPresenter extends BaseSourcePresenter<User,User,ContractDataSource,ContactContract.View>
        implements ContactContract.Presenter ,DataSource.SucceedCallback<List<User>>{


    public ContactPresenter(ContactContract.View view) {
        super(new ContractRepository(),view);
    }


    @Override
    public void start() {
        super.start();
        // 加载网络数据
        UserHelper.refreshContacts();
    }

    @Override
    public void onDataLoaded(List<User> users) {
        //最终到会调用到这里来

        final ContactContract.View view = getView();
        if (view==null){
            return;
        }

        RecyclerAdapter<User> adapter = view.getRecyclerAdapter();
        List<User> old = adapter.getItems();
        //进行数据对比
        DiffUtil.Callback callback = new DiffUiDataCallback<>(old,users);
        DiffUtil.DiffResult result = DiffUtil.calculateDiff(callback);
        //调用基类方法进行界面刷新
        refreshData(result,users);
    }
}
